# p05_gen_gtc.sh
# - 描述: 利用地理编码表生成GTC影像
# - 作者: iam002
# - 日期: 2025年2月24日
# - 版本: V1.0

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 读取全局变量
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if [ ! -f "$1" ]; then
    source p00_global_params.sh
else
    source $1
fi

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# 日志路径
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
cur_script_name=$(basename "$0" .sh)
out_log_path=$in_output_folder/$cur_script_name".log"

# 程序接口说明:
# usage: geocode_back <data_in> <width_in> <lookup_table> <data_out> <width_out> [nlines_out] [interp_mode] [dtype] [lr_in] [lr_out] [order] [e_flag]
#
# input parameters:
#   data_in       (input) data file (format as specified by format_flag parameter)
#   width_in      width of input data file
#   lookup_table  (input) lookup table containing pairs of real-valued input data coordinates
#   data_out      (output) output data file
#   width_out     width of gc_map lookup table, output file has the same width
#   nlines_out    number of lines of output data file (enter - or 0 for default: number of lines in gc_map)
#   interp_mode   interpolation mode (enter - for default)
#                   0: nearest-neighbor
#                   1: bicubic spline (default)
#                   2: bicubic-log spline, interpolates log(data)
#                   3: bicubic-sqrt spline, interpolates sqrt(data)
#                   4: B-spline interpolation (default B-spline degree: 5)
#                   5: B-spline interpolation sqrt(x) (default B-spline degree: 5)
#                   6: Lanczos interpolation (default Lanczos function order: 5)
#                   7: Lanczos interpolation sqrt(x) (default Lanczos function order: 5)
#                 NOTE: log and sqrt interpolation modes should only be used with non-negative data!
#   dtype         input/output data type (enter - for default)
#                   0: FLOAT (default)
#                   1: FCOMPLEX
#                   2: SUN/BMP/TIFF 8 or 24-bit raster image
#                   3: UNSIGNED CHAR
#                   4: SHORT
#                   5: DOUBLE
#   lr_in         input  SUN/BMP/TIFF raster image flipped left/right (enter - for default: 1: not flipped (default), -1: flipped)
#   lr_out        output SUN/BMP/TIFF raster image flipped left/right (enter - for default: 1: not flipped (default), -1: flipped)
#   order         Lanczos function order or B-spline degree (2->9) (enter - default: 5)
#   e_flag        extrapolation flag (enter - for default)
#                   0: do not extrapolate (default)
#                   1: extrapolate up to 0.5 pixels beyond input edges
#
#   NOTE: see documentation for information on recommended interpolation modes for different data types

# p01_sar_multilook.sh 结果
out_mli_path=$in_output_folder/$in_produce_name".mli"
out_mli_par_path=$in_output_folder/$in_produce_name".mli_par"

# p02_dem_resamp.sh 结果
out_resamp_dem_par_path=$in_output_folder/$in_produce_name"_resamp.dem_par"

# p04_refine_lkt.sh 结果
out_fine_lkt_path=$in_output_folder/$in_produce_name.fine_lkt

# 获取图像大小
dem_width=$(grep -oP '^width:\s*\K\d+' "$out_resamp_dem_par_path")
mli_width=$(grep -oP '^range_samples:\s*\K\d+' "$out_mli_par_path")

out_fine_gtc_path=$in_output_folder/$in_produce_name.gtc

geocode_back $out_mli_path $mli_width $out_fine_lkt_path $out_fine_gtc_path $dem_width - 1 0 
